package com.leenleda.ips.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.leenleda.ips.R;
import com.leenleda.ips.activity.adapter.CrashListAdapter;
import com.leenleda.ips.databinding.ActivityCrashBinding;
import com.leenleda.ips.receiver.Constant;
import com.leenleda.ips.util.LogUtils;
import com.leenleda.ips.util.TimeUtils;
import com.leenleda.ips.util.UShared;
import com.leenleda.ips.util.Utils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CrashActivity extends AppCompatActivity {

    private ActivityCrashBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window _window = getWindow();
        WindowManager.LayoutParams params = _window.getAttributes();
//        params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_IMMERSIVE;
        params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
        _window.setAttributes(params);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);// 隐藏状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);//禁用键盘

//        setContentView(R.layout.dialog_tips);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_crash);
        getFileList();
        initAdapter();
        String endPublish = "<=认证时间=>" + UShared.getString(Constant.WEB_LOAD_TIME) + "\n"
                + "<=当前时间=>" + TimeUtils.stampToDate(System.currentTimeMillis()) + "\n"
                + "<=当前发布任务=>" + UShared.getString(Constant.WEB_LOAD_END) + "\n"
                + "<=设备认证=>" + UShared.getString(Constant.WEB_LOAD_GET);
        binding.endPublish.setText(endPublish);
    }

    /**
     * 右上角返回键
     */
    public void btnBack(View view){
        finish();
    }

    private List<String> dataList = new ArrayList<>();
    private void getFileList(){
        dataList.clear();
        String path = null;
        try {
            path = Environment.getExternalStorageDirectory().getCanonicalPath() + "/"
                    + "LELDIPS/Crash/";
            File file = new File(path);

            if (file != null && file.isDirectory()) {
                String[] children = file.list();
                LogUtils.e("getFileList", "-------getFileList---1-----" + file);
                for (int i = 0; i < children.length; i++) {
                    LogUtils.e("getFileList", "-------getFileList----2----" + children[i]);
                    dataList.add(children[i]);
                }
                return;
            }
            LogUtils.e("getFileList", "-------getFileList---3-----" + file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private CrashListAdapter adapter;
    private void initAdapter(){
        adapter = new CrashListAdapter(this, dataList);
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        binding.recyclerView.setAdapter(adapter);
        adapter.setOnItemClick(name -> {
            Intent intent = new Intent(CrashActivity.this, StrActivity.class);
            intent.putExtra("name", name);
            Utils.startActivity(CrashActivity.this, intent);
        });
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        LogUtils.e("BaseClick", "==keyCode===" + keyCode);
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            LogUtils.e("LSNVSDLV", "=====takeScreenShot===111=====");
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                LogUtils.e("LSNVSDLV", "=====takeScreenShot===222=====");
//                takeScreenShot();
            }
            btnBack(null);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
